/***************************************************************************
 创建者：       hualei
 开始时间：    2018.09.10
 copyright:(C) 深圳市华友高科有限公司
 修改说明：（每次有修改就添加一条，带有 修改人，修改时间，修改描述）

 ***************************************************************************/
#include "modbuswriteinstructiondelegate.h"
#include <QDebug>
ModbusWriteInstructionDelegate::ModbusWriteInstructionDelegate(QWidget *parent)
    : QObject(parent)
{
    writeStag=0;
}

void ModbusWriteInstructionDelegate::initView()
{
    modbusWriteInstructionView->noteStr = "(No NoteStr!)";
    modbusWriteInstructionView->writeConstValue = writeStag;
    modbusWriteInstructionView->writeValue = writeStag;
    modbusWriteInstructionView->writeRegAddress =1;
    modbusWriteInstructionView->modbusID ="1";
    modbusWriteInstructionView->selectionModbusMode = E_SELECTION_INTERNET;
    modbusWriteInstructionView->selectionModbusType = E_SELECTION_MASTER;
    modbusWriteInstructionView->modbusWriteType = E_MODBUS_WRITE_TYPE_INTERNET_MASTER;
    modbusWriteInstructionView->JMP_LB_Value = "1";

}

void ModbusWriteInstructionDelegate::finishEditInstructionSlot()
{
    QString str = "MODBUS_WRITE ";

    if(modbusWriteInstructionView->selectionWriteValueType == E_SELECTION_CONST)
    {
        str += "," + QString::number(modbusWriteInstructionView->writeConstValue) + ",";
        writeStag = modbusWriteInstructionView->writeValue;
    }
    else if(modbusWriteInstructionView->selectionWriteValueType == E_SELECTION_NO_CONST)
    {
        str += ",R[" + QString::number(modbusWriteInstructionView->writeValue) + "],";
        writeStag = modbusWriteInstructionView->writeConstValue;
    }

    str += QString::number(modbusWriteInstructionView->writeRegAddress) + ",";
    str += modbusWriteInstructionView->modbusID + ",";

    QString modbusTypeStr;
    QString modbusModeStr;
    if(modbusWriteInstructionView->selectionModbusType == E_SELECTION_INTERNET)
    {
        modbusTypeStr = "INTERNET, ";
    }
    else if(modbusWriteInstructionView->selectionModbusType == E_SELECTION_RS485)
    {
        modbusTypeStr = "RS485, ";
    }

    if(modbusWriteInstructionView->selectionModbusMode == E_SELECTION_MASTER)
    {
        modbusModeStr = "MASTER, ";
    }
    if(modbusWriteInstructionView->selectionModbusMode == E_SELECTION_SLAVE)
    {
        modbusModeStr = "SLAVE, ";
    }

    str += modbusTypeStr + modbusModeStr;
    str += "JMP LB[" + modbusWriteInstructionView->JMP_LB_Value + "]  //";
    str += modbusWriteInstructionView->noteStr;

    modbusWriteInstructionView->instruction = str;

}

void ModbusWriteInstructionDelegate::cleanNoteSlot()
{
    modbusWriteInstructionView->noteStr.clear();
    qDebug() << "ModbusWriteInstructionDelegate Clean NoteStr!";
}
